package easy.id_0_100;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class hua10 {
    public static void main(String[] args) {
        String str= "a2CB1c";
        String str2 = "ab12C4Ac3B";
        System.out.println(characterSort(str2));
    }


    private static String characterSort(String inputStr) {
        ArrayList<Character> letter=new ArrayList<>();
        ArrayList<Character> num=new ArrayList<>();
        for(int i=0;i<inputStr.length();i++){
            if(inputStr.charAt(i)>='0' && inputStr.charAt(i)<='9'){
                num.add(inputStr.charAt(i));
            }else{
                letter.add(inputStr.charAt(i));
            }
        }
        Comparator<Character> customComparator = new Comparator<Character>() {
            @Override
            public int compare(Character c1, Character c2) {
                if (Character.isLowerCase(c1) && Character.isLowerCase(c2)) {
                    return c1.compareTo(c2); // 小写字母按照a-z的顺序
                } else if (Character.isUpperCase(c1) && Character.isUpperCase(c2)) {
                    return c1.compareTo(c2); // 大写字母按照A-Z的顺序
                } else {
                    // 大写字母大于小写字母
                    if (Character.isUpperCase(c1)) {
                        return 1; // c1大于c2
                    } else {
                        return -1; // c1小于c2
                    }
                }
            }
        };
        Collections.sort(letter, customComparator);
        // 自定义比较器
        Comparator<Character> customComparator2 = new Comparator<Character>() {
            @Override
            public int compare(Character c1, Character c2) {
                return Character.compare(c1, c2); // 使用Character的比较方法比较字符
            }
        };
        Collections.sort(num, customComparator2);
        String result="";
        int indexnum=0,indexletter=0;
        for(int i=0;i<inputStr.length();i++){
            if(inputStr.charAt(i)>='0' && inputStr.charAt(i)<='9'){
                result=result+num.get(indexnum++).toString();
            }else{
                result=result+letter.get(indexletter++).toString();
            }
        }
        return result;
    }
}
